import Api from '../../api/index.js'

export default {
    state: {
        userInfo: {
            id: 0,
            userName: 'admin'
        },
        useNormalRouteView: false,
        relocalMenu: false, //是否重新定位左边菜单
        permission: []
    },
    getters: {
        getUserInfo: state => {
            return state.userInfo;
        },
        getIsLogin: state => {
            return true;
            if (state.userInfo && state.userInfo.id > 0) {
                return true;
            }
            return false;
        },
        getUseNormalRouteView: state => {
            return state.useNormalRouteView;
        },
        getRelocalMenu: state => {
            return state.relocalMenu;
        }
    },
    mutations: {
        setUserInfo(state, model) {
            state.userInfo = model;
        },
        setNormalRouteView(state, value) {
            state.useNormalRouteView = value;
        },
        setRelocalMenu(state, value) {
            state.relocalMenu = value;
        },
        setPermission(state, permission) {
            state.permission = permission;
        }
    },
    actions: {
        dealPermission({ commit }, token) {
            commit('setUserInfo', {});
            commit('setPermission', []);
            return new Promise((resolve, reject) => {
                let res = {
                    id: 1,
                    name: 'admin',
                    resources: [{
                            name: 'index',
                            path: '/index',
                            children: []
                        },
                        {
                            name: 'roleManages',
                            path: '/roleManage',
                            resourceCode: 'guomao:roleManages',
                            children: [{
                                name: 'roleManage',
                                path: '/roleManage',
                                resourceCode: 'guomao:roleManage',
                            }]
                        },
                        {
                            name: 'carManages',
                            path: '/carManage',
                            resourceCode: 'guomao:carManages',
                            children: [{
                                name: 'carManage',
                                path: '/carManage',
                                resourceCode: 'guomao:carManage',
                            }]
                        }, {
                            name: 'userManages',
                            path: '/userManage',
                            resourceCode: 'guomao:userManages',
                            children: [{
                                name: 'userManage',
                                path: '/userManage',
                                resourceCode: 'guomao:userManage',
                            }]
                        },
                    ],
                }
                sessionStorage.userid = res.id;
                sessionStorage.username = res.name;
                if (!res.id) {
                    reject(false);
                    return;
                }
                let userInfo = {
                    id: res.id,
                    userName: res.name
                };
                commit('setUserInfo', userInfo);
                let permission = [];
                dealPermission(res.resources);

                function dealPermission(trees) {
                    trees.forEach(v => {
                        permission.push(v)
                            // console.log(permission);
                        if (v.children && v.children.length > 0) {
                            dealPermission(v.children)
                        }
                    })
                }
                commit('setPermission', permission);
                resolve(true);
            })
        }
    }
}